package com.uenpay.im.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.provider.MediaStore.Images.Thumbnails;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;

import com.qyx.android.weight.choosemorepic.PhotoAibum;
import com.qyx.android.weight.choosemorepic.PhotoItem;
import com.uenpay.im.ui.old.activity.choosemorepic.PhotoGridItem;

/**
 * @author SL
 * @date 2014/7/24
 * @copyRight http://www.qiyunxin.com
 */
public class LocalImageAdapter extends BaseAdapter {

	private Context context;
	private PhotoAibum aibum;
	private PhotoGridItem item;

	public LocalImageAdapter(Context context, PhotoAibum aibum) {
		this.context = context;
		this.aibum = aibum;
	}

	public void setSelectImage(int index){
		for(int i = 0,len = aibum.getBitList().size(); i < len; i++) {
			aibum.getBitList().get(i).setSelect(false);
		}
		aibum.getBitList().get(index).setSelect(true);
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return aibum.getBitList().size();
	}

	@Override
	public PhotoItem getItem(int position) {
		return aibum.getBitList().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			item = new PhotoGridItem(context);
			item.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT));
		} else {
			item = (PhotoGridItem) convertView;
		}
		Bitmap bitmap = Thumbnails.getThumbnail(
				context.getContentResolver(), aibum.getBitList().get(position)
						.getPhotoID(), Thumbnails.MICRO_KIND, null);
		item.SetBitmap(bitmap);

		boolean flag = aibum.getBitList().get(position).isSelect();
		item.setChecked(flag);
		return item;
	}

}
